package com.tju.resource.oauth.controller.log;

import com.tju.resource.business.log.LogProcessor;
import com.tju.resource.common.ServiceException;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * 日志记录查询
 *
 * @Author CLS
 * @Date 2022/4/26 11:42
 */
@RestController
@RequestMapping("log")
public class LoginQueryController {

    @Autowired
    private ChineseExceptionUtils zh_ch;

    @Autowired
    private LogProcessor processor;

    @PostMapping("/loginQuery")
    @PreAuthorize("hasAuthority('sys:manage:log:login:list')")
    public Result<Object> loginQuery(@RequestBody ClientParameters.Log log) {
        return Optional.ofNullable(log)
                .filter(l -> l.getLimit() > 0)
                .map(l -> processor.getLoginLog(l))
                .orElseThrow(() -> new ServiceException(zh_ch.AbnormalOperation));
    }

    @PostMapping("/operQuery")
    @PreAuthorize("hasAuthority('sys:manage:log:oper:list')")
    public Result<Object> operQuery(@RequestBody ClientParameters.Log log) {
        return processor.getOperationLog(log);
    }
}
